package android;

import java.util.Scanner;

public class TicTacToe {

	/**
	 * @param args
	 */
	private static void fPrint(char[][] f) {
		for (int i = 0; i < f.length; i++) {
			for (int j = 0; j < f[i].length; j++) {
				System.out.print("   " + f[i][j] + (j < 2 ? "\t|" : ""));
			}
			if (i < 2)
				System.out.println("\n__________________________\n");
		}
		System.out.println("\n***********************************");
	}

	private static void makeTurn(char[][] f) {
		if (f[1][1] == 0) {
			f[1][1] = 'O';
			return;
		} else {
			for (int i = 0; i < 3; i++) {
				if (i != 1 && f[i][2 - i] == f[1][1] && f[2 - i][i] == 0) {
					f[2 - i][i] = 'O';
					return;
				} else if (i != 1 && f[i][i] == f[1][1] && f[2 - i][2 - i] == 0) {
					f[2 - i][2 - i] = 'O';
					return;
				} else if (f[i][0] == f[i][1] && f[i][0] != 0 && f[i][2] == 0) {
					f[i][2] = 'O';
					return;
				} else if (f[i][0] == f[i][2] && f[i][0] != 0 && f[i][1] == 0) {
					f[i][1] = 'O';
					return;
				} else if (f[i][1] == f[i][2] && f[i][1] != 0 && f[i][0] == 0) {
					f[i][0] = 'O';
					return;
				} else if (f[0][i] == f[1][i] && f[0][i] != 0 && f[2][i] == 0) {
					f[2][i] = 'O';
					return;
				} else if (f[1][i] == f[2][i] && f[1][i] != 0 && f[0][i] == 0) {
					f[0][i] = 'O';
					return;
				} else if (f[0][i] == f[2][i] && f[0][i] != 0 && f[1][i] == 0) {
					f[1][i] = 'O';
					return;
				}

			}
			for (int i = 0; i < 3; i++) {
				if (f[i][i] == 0) {
					f[i][i] = 'O';
					return;
				} else if (f[i][2 - i] == 0) {
					f[i][2 - i] = 'O';
					return;
				}
			}
		}
	}

	private static int getInt() {
		Scanner sc = new Scanner(System.in);
		while (!sc.hasNextInt()) {
			System.out.println("Wrong input \"" + sc.nextLine()
					+ "\". Enter a number.");
		}
		return sc.nextInt();
	}

	private static char check(char[][] f) {
		if ((f[0][0] == f[1][1] && f[1][1] == f[2][2])
				|| (f[2][0] == f[1][1] && f[1][1] == f[0][2])) {
			return f[1][1];
		} else {
			for (int i = 0; i < 3; i++) {
				if ((f[i][0] == f[i][1] && f[i][1] == f[i][2])
						|| (f[0][i] == f[1][i] && f[1][i] == f[2][i])) {
					return f[i][i];
				}
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		int x = 0, y = 0;
		char[][] f = new char[3][3];
		boolean ai = false;
		int turn = 0;
		System.out.print("Select number of players 1 or 2:\t");
		ai = getInt() == 1;
		System.out.println((ai ? 1 : 2) + " player(s) game");
		while (check(f) == 0 && turn < 9) {
			if (ai && turn % 2 != 0) {
				makeTurn(f);
				fPrint(f);
				turn++;
				continue;
			}
			x = 999;
			y = 999;
			System.out.println("\nPlayer" + (turn % 2 == 0 ? 1 : 2) + " turn");
			System.out.print("row = ");
			x = getInt() - 1;
			System.out.print("column = ");
			y = getInt() - 1;
			if (x + y >= 0 && x < 3 && y < 3 && f[x][y] == 0) {
				f[x][y] = (turn % 2 == 0 ? 'X' : 'O');
				fPrint(f);
				turn++;
			} else
				System.out.println("Wrong input. Try again.");
		}
		x = check(f);
		System.out.println(x != 0 ? "\nPlayer" + (x == 'X' ? 1 : 2) + " win!"
				: "\nDraw");
	}

}
